package org.siberia.server.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.siberia.client.bean.ActionSuiteDTO;
import org.siberia.client.bean.UserDTO;
import org.siberia.server.converter.ActionSuiteConverter;
import org.siberia.server.model.ActionSuite;
import org.siberia.server.model.User;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ActionSuiteServiceImpl extends RemoteServiceServlet implements org.siberia.client.service.ActionSuiteService
{

	public ActionSuiteDTO getSuiteById(Long id)
	{
		ActionSuiteDTO result = null;
		
		ActionSuite _suite = ActionSuite.findActionSuite(id);
		
		if ( _suite != null )
		{
			result = ActionSuiteConverter.convert(_suite);
		}
		
		return result;
	}
	
	public void createUser(ActionSuiteDTO suite)
	{
		ActionSuite _suite = ActionSuiteConverter.convert(suite);
		_suite.persist();
	}
	
	public List<ActionSuiteDTO> findAllForUser(UserDTO user)
	{
		List<ActionSuiteDTO> _suites = null;
		
		if ( user != null && user.id != null )
		{
			List<ActionSuite> tmpSuites = ActionSuite.findSuitesForUser(user.id);
			
			if ( tmpSuites != null && ! tmpSuites.isEmpty() )
			{
				_suites = new ArrayList<ActionSuiteDTO>(tmpSuites.size());
				
				for (ActionSuite actionSuite : tmpSuites)
				{
					_suites.add( ActionSuiteConverter.convert(actionSuite) );
				}
			}
		}
		
		if ( _suites == null )
		{
			_suites = Collections.<ActionSuiteDTO>emptyList(); 
		}
		
		return _suites;
	}
	
	public void deleteAll()
	{
		ActionSuite.deleteAll();
	}
}
